package com.sen.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sen.entity.MenuEntity;
import com.sen.entity.MenuRoleEntity;
import com.sen.entity.RoleEntity;
import com.sen.service.MenuRoleService;
import com.sen.service.RoleService;
import com.sen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.relation.Role;
import java.util.*;

/**
 * 职位表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-08 17:54:40
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuRoleService menuRoleService;

    @RequestMapping("/queryAll")
    public R queryAll(){
        List<RoleEntity> roleList = roleService.list();
        return R.ok().put("roleList",roleList);
    }

    /**
     * 获取用户拥有的权限
     * @param id
     * @return
     */
    @RequestMapping("/getRoleMenu")
    public R getRoleMenu(String id){
        List<RoleEntity> roleList = roleService.getRoleByid(id);
        return R.ok().put("roleList",roleList);
    }

    /**
     * 更改用户权限
     * @param id
     * @return
     */
    @RequestMapping("/updRoleMenu")
    public R updRoleMenu(String id,String menuList){

        //添加新的权限
        menuRoleService.addRoleMenu(id, menuList);
        return R.ok();
    }

    /**
     * 添加角色权限
     * @param rolename
     * @param remark
     * @param menuList
     * @return
     */
    @RequestMapping("/addRoleMenu")
    public R addRoleMenu(String rolename,String remark,String menuList){

        //添加新的权限
        menuRoleService.addRoleMenu(rolename,remark, menuList);
        return R.ok();
    }

    @RequestMapping("/updState")
    public void updState(String state,String id){
        roleService.update(new UpdateWrapper<RoleEntity>().set("state", state).eq("id", id));
    }
}
